package com.taptech.doufu.util;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

import com.taptech.doufu.ui.activity.NewAlbumsActivity;
import com.taptech.doufu.ui.activity.AnswerStarActivity;
import com.taptech.doufu.ui.activity.BrowseActivity;
import com.taptech.doufu.ui.activity.EasyBrowseActivity;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.bean.AlbumsBean;
import com.taptech.doufu.bean.DFHomeBannerBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.services.CartoonServices;
import com.taptech.doufu.services.CpService;
import com.taptech.doufu.services.PersonalNoteService;

/**
 * 工具类
 */

public class AppUtil {
    /**
     * 旋转一个 imageview
     *
     * @param imageView
     */
    public static void ratateImageView(ImageView imageView) {
        Animation animation = new RotateAnimation(0, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(500);
        imageView.startAnimation(animation);
    }

    /**
     * 判断网络状态
     * @param context
     * @return
     */
    public static boolean IsNetworkConnected(Context context){
        ConnectivityManager conMgr =  (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo i = conMgr.getActiveNetworkInfo();
        if (i == null)
            return false;
        if (!i.isConnected())
            return false;
        if (!i.isAvailable())
            return false;

        return true;
    }

    /**
     * 轮播图跳转逻辑
     * @param mContext
     * @param bean
     */
    public static void intentBanner(Context mContext, DFHomeBannerBean bean) {
        Intent intent = new Intent();
        switch (DiaobaoUtil.String2Int(bean.getObject_type())) {
            case Constant.OBJECT_TYPE_NOVEL://18小说
                StartActivityUtil.novelOnclick(mContext, bean.getId());
                break;

            case Constant.OBJECT_TYPE_ANSWER://腐化度测试
                if (AccountService.getInstance().isLogin()) {
                    intent.setClass(mContext, AnswerStarActivity.class);
                    mContext.startActivity(intent);
                } else {
                    AccountService.getInstance().jumpToLogin();
                }
                break;
            case Constant.OBJECT_TYPE_BROWSE:// 跳转到内部网页的类型
                String url = bean.getUrl();
                String title = bean.getTitle();
                intent.setClass(mContext, BrowseActivity.class);
                intent.putExtra(Constant.URL, url);
                intent.putExtra(Constant.TITLE, title);
                mContext.startActivity(intent);
                break;
            case Constant.OBJECT_TYPE_EASY_WEBVIEW:
                intent.setClass(mContext, EasyBrowseActivity.class);
                intent.putExtra(Constant.URL, bean.getUrl());
                intent.putExtra(Constant.TITLE, bean.getTitle());
                mContext.startActivity(intent);
                break;
            case 54:
                intent.setClass(mContext, EasyBrowseActivity.class);
                intent.putExtra(Constant.URL, bean.getUrl());
                intent.putExtra(Constant.TITLE, bean.getTitle());
                mContext.startActivity(intent);
                break;
            case Constant.OBJECT_TYPE_ALBUMS:
                AlbumsBean albumsBean = new AlbumsBean();
                albumsBean.setType(bean.getType());
                albumsBean.setName(bean.getTitle());
                albumsBean.setUrl(bean.getUrl());
                StartActivityUtil.albumsOnclick(mContext, albumsBean);
                break;
            case Constant.CONTENT_TYPE_TOPIC:
                switch (DiaobaoUtil.String2Int(bean.getTopic_type())) {
                    case Constant.OBJECT_TYPE_NOVEL:
                        StartActivityUtil.novelOnclick(mContext, bean.getId());
                        break;

                    case Constant.OBJECT_TYPE_ANSWER:
                        if (AccountService.getInstance().isLogin()) {
                            intent.setClass(mContext, AnswerStarActivity.class);
                            mContext.startActivity(intent);
                        } else {
                            AccountService.getInstance().jumpToLogin();
                        }
                        break;
                    case Constant.OBJECT_TYPE_SWEEP:
                        if (AccountService.getInstance().isLogin()) {
                            int type = DiaobaoUtil.String2Int(bean.getType());
                            intent.setClass(mContext, NewAlbumsActivity.class);
                            intent.putExtra(Constant.URL, bean.getUrl());
                            intent.putExtra(Constant.TYPE, type);
                            intent.putExtra(Constant.NAME, bean.getName());
                            mContext.startActivity(intent);
                        } else {
                            AccountService.getInstance().jumpToLogin();
                        }
                        break;
                    default:
                        break;

                }
                break;
            case Constant.OBJECT_TYPE_NOTE:
            case Constant.OBJECT_TYPE_DRAW:
            case Constant.OBJECT_TYPE_COS:
                PersonalNoteService.enterPersonalNoteDesActivity(mContext, bean.getId(), bean.getObject_type());
                break;
            case Constant.OBJECT_TYPE_CP:
                CpService.enterCpDesActivity(mContext, bean.getId());
                break;
            case Constant.OBJECT_TYPE_CARTOON:
                CartoonServices.enterCartoonDes(mContext, bean.getId());
                break;
            case Constant.OBJECT_TYPE_WEEX:
                SimpleWeexActivity.startActivity(mContext,bean.getUrl());
                break;

            default:
                if(bean!=null && !TextUtils.isEmpty(bean.getUrl()) && bean.getUrl().contains("/weex/")){
                    SimpleWeexActivity.startActivity(mContext,bean.getUrl());
                }
                break;
        }


    }


}
